package cn.edu.szpt.qqdemov3;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.szpt.qqdemov3.beans.QQContactBean;
import cn.edu.szpt.qqdemov3.dbutils.MyDbHelper;

public class LoginActivity extends AppCompatActivity {
    private TextView tvForgetPwd;
    private EditText etQQNum;
    private EditText etQQPwd;
    private Button btnLogin;
    private CheckBox chkRememberPwd;
    public static String PHOTO_URL;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        etQQNum = findViewById(R.id.etQQNum);
        etQQPwd = findViewById(R.id.etQQPwd);
        btnLogin = findViewById(R.id.btnLogin);
        chkRememberPwd = findViewById(R.id.chkRememberPwd);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                String qqnum = etQQNum.getText().toString().trim();
                String pwd = etQQPwd.getText().toString().trim();

                MyDbHelper helper = new MyDbHelper(LoginActivity.this);
                SQLiteDatabase db = helper.getReadableDatabase();

                String sql = "select * from QQ_Login where qq_num = ? and qq_pwd = ?";

                Cursor cursor = db.rawQuery(sql,new String[]{qqnum,pwd});

                if(cursor.moveToNext()){
                    //登录成功
                    if(chkRememberPwd.isChecked()){
                        SharedPreferences settings = getSharedPreferences("setting",MODE_PRIVATE);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString("qqnum",etQQNum.getText().toString().trim());
                        editor.putString("pwd",etQQPwd.getText().toString().trim());
                        editor.commit();
                    }

                    MainActivity.loginedUser = new QQContactBean(cursor.getString(cursor.getColumnIndex("qq_num")),
                                                                 cursor.getString(cursor.getColumnIndex("qq_name")),
                                                                 cursor.getString(cursor.getColumnIndex("qq_imgurl")),
                                                                 cursor.getString(cursor.getColumnIndex("qq_online")),
                                                                 cursor.getString(cursor.getColumnIndex("qq_action")),
                                                                 cursor.getString(cursor.getColumnIndex("belong_country")));

                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }else{
                    //登录失败
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
                }



            }
        });

        tvForgetPwd = findViewById(R.id.tvForgetPwd);
        tvForgetPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(LoginActivity.this,ForgetPwdActivity.class);
//                startActivity(intent);

                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
//                Uri data = Uri.parse("https://www.baidu.com");
                Uri data =Uri.parse("tel:12345678");
                intent.setData(data);
                startActivity(intent);

            }
        });

        SharedPreferences settings = getSharedPreferences("setting",MODE_PRIVATE);
        etQQNum.setText(settings.getString("qqnum",""));
        etQQPwd.setText(settings.getString("pwd",""));

        PHOTO_URL = getExternalFilesDir("photos").toString();
//        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();

        //进行动态授权
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1000);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1000:
                if(grantResults.length==0 || grantResults[0]!=PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this, "请授予读取联系人信息的权限", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}